import { Module, NestModule, MiddlewareConsumer } from '@nestjs/common';
import { ConfigModule } from '@nestjs/config';
import { ThrottlerModule } from '@nestjs/throttler';
import { AppController } from './app.controller';
import { AppService } from './app.service';
import { AuthModule } from './auth/auth.module';
import { PrismaModule } from './prisma/prisma.module';
import { ShortsModule } from './shorts/shorts.module';
import { ArticlesModule } from './articles/articles.module';
import { VipModule } from './vip/vip.module';
import { ShareModule } from './share/share.module';
import { JwtMiddleware } from './auth/middleware/jwt.middleware';
import { UserModule } from './user/user.module';

@Module({
  imports: [
    ConfigModule.forRoot({
      isGlobal: true,
      envFilePath: '.env',
    }),
    ThrottlerModule.forRoot([
      {
        ttl: 60000, // 1分钟
        limit: 10, // 最多10次请求
      },
    ]),
    PrismaModule,
    AuthModule,
    ShortsModule,
    ArticlesModule,
    VipModule,
    ShareModule,
    UserModule,
  ],
  controllers: [AppController],
  providers: [AppService],
})
export class AppModule implements NestModule {
  configure(consumer: MiddlewareConsumer) {
    // 全局应用JWT中间件
    consumer.apply(JwtMiddleware).forRoutes('*'); // 应用到所有路由
  }
}
